/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "app_sys.h"
#include "protocol_link.h"
#include "protocol_cmd.h"
#include "elog.h"
#include "board.h"
#include "app_cmd_debug.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for startup */
osThreadId_t startupHandle;
const osThreadAttr_t startup_attributes = {
  .name = "startup",
  .stack_size = 768 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for display */
osThreadId_t displayHandle;
const osThreadAttr_t display_attributes = {
  .name = "display",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for communicate */
osThreadId_t communicateHandle;
const osThreadAttr_t communicate_attributes = {
  .name = "communicate",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};
/* Definitions for app_sys */
osThreadId_t app_sysHandle;
const osThreadAttr_t app_sys_attributes = {
  .name = "app_sys",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for adc_processing */
osThreadId_t adc_processingHandle;
const osThreadAttr_t adc_processing_attributes = {
  .name = "adc_processing",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};
/* Definitions for debug_console */
osThreadId_t debug_consoleHandle;
const osThreadAttr_t debug_console_attributes = {
  .name = "debug_console",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for mutex_console */
osMutexId_t mutex_consoleHandle;
const osMutexAttr_t mutex_console_attributes = {
  .name = "mutex_console"
};
/* Definitions for adc1_trans_sem */
osSemaphoreId_t adc1_trans_semHandle;
const osSemaphoreAttr_t adc1_trans_sem_attributes = {
  .name = "adc1_trans_sem"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
/* Hook prototypes */
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName);
void vApplicationMallocFailedHook(void);

void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName)
{
   /* Run time stack overflow checking is performed if
   configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is
   called if a stack overflow is detected. */
   log_e("task stack overflow taskHandle: %d, taskName: %s!!!", xTask, pcTaskName);
   for (; ;) {
      static uint32_t test_tick = 0;
      test_tick++;
      if (test_tick > 1000)
      {
          test_tick = 0;
          log_e("task stack overflow taskHandle: %d, taskName: %s!!!", xTask, pcTaskName);
      }
   }
}

void vApplicationMallocFailedHook(void)
{
   /* vApplicationMallocFailedHook() will only be called if
   configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
   function that will get called if a call to pvPortMalloc() fails.
   pvPortMalloc() is called internally by the kernel whenever a task, queue,
   timer or semaphore is created. It is also called by various parts of the
   demo application. If heap_1.c or heap_2.c are used, then the size of the
   heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
   FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
   to query the size of free heap space that remains (although it does not
   provide information on how the remaining heap might be fragmented). */
   log_e("application malloc falied!!!");
   for (; ;) {

   }
}
/* USER CODE END FunctionPrototypes */

void task_startup(void *argument);
void task_display(void *argument);
void task_communicate(void *argument);
void task_app_sys(void *argument);
void task_adc_processing(void *argument);
void task_debug_console(void *argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
  /* elog init */
  app_sys_log_init();

  /* boot init */
  bsp_board_init();

  /* sys init */
  app_sys_para_init();

  /* USER CODE END Init */
  /* Create the mutex(es) */
  /* creation of mutex_console */
  mutex_consoleHandle = osMutexNew(&mutex_console_attributes);

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* creation of adc1_trans_sem */
  adc1_trans_semHandle = osSemaphoreNew(1, 0, &adc1_trans_sem_attributes);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of startup */
  startupHandle = osThreadNew(task_startup, NULL, &startup_attributes);

  /* creation of display */
  displayHandle = osThreadNew(task_display, NULL, &display_attributes);

  /* creation of communicate */
  communicateHandle = osThreadNew(task_communicate, NULL, &communicate_attributes);

  /* creation of app_sys */
  app_sysHandle = osThreadNew(task_app_sys, NULL, &app_sys_attributes);

  /* creation of adc_processing */
  adc_processingHandle = osThreadNew(task_adc_processing, NULL, &adc_processing_attributes);

  /* creation of debug_console */
  debug_consoleHandle = osThreadNew(task_debug_console, NULL, &debug_console_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  osThreadSuspend(displayHandle);
  osThreadSuspend(communicateHandle);
  osThreadSuspend(app_sysHandle);
  osThreadSuspend(adc_processingHandle);
  osThreadSuspend(debug_consoleHandle);
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_task_startup */
/**
  * @brief  Function implementing the startup thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_task_startup */
void task_startup(void *argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();
  /* USER CODE BEGIN task_startup */

  osDelay(1000);
  osThreadResume(displayHandle);
  osThreadResume(communicateHandle);
  osThreadResume(app_sysHandle);
  osThreadResume(adc_processingHandle);
  osThreadResume(debug_consoleHandle);

  osThreadExit();
  
  /* Infinite loop */
  for(;;)
  {
    log_e("can not run to here!");
    osDelay(1);
  }
  /* USER CODE END task_startup */
}

/* USER CODE BEGIN Header_task_display */
/**
* @brief Function implementing the display thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_task_display */
void task_display(void *argument)
{
  /* USER CODE BEGIN task_display */
  /* Infinite loop */
  for(;;)
  {
    // all_display_digital(app_sys_para.display_value);
    all_display_digital_signed(app_sys_para.display_value);
    osDelay(3);
  }
  /* USER CODE END task_display */
}

/* USER CODE BEGIN Header_task_communicate */
/**
* @brief Function implementing the communicate thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_task_communicate */
void task_communicate(void *argument)
{
  /* USER CODE BEGIN task_communicate */
  protocol_link_init();
  /* Infinite loop */
  for(;;)
  {
    protocol_cmd_handle();
    osDelay(100);
  }
  /* USER CODE END task_communicate */
}

/* USER CODE BEGIN Header_task_app_sys */
/**
* @brief Function implementing the app_sys thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_task_app_sys */
void task_app_sys(void *argument)
{
  /* USER CODE BEGIN task_app_sys */
  /* Infinite loop */
  // for(;;)
  // {
  //   osDelay(1);
  // }
  app_sys_task();
  /* USER CODE END task_app_sys */
}

/* USER CODE BEGIN Header_task_adc_processing */
/**
* @brief Function implementing the adc_processing thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_task_adc_processing */
void task_adc_processing(void *argument)
{
  /* USER CODE BEGIN task_adc_processing */
  /* Infinite loop */
  app_adc_processing_task();
  /* USER CODE END task_adc_processing */
}

/* USER CODE BEGIN Header_task_debug_console */
/**
* @brief Function implementing the debug_console thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_task_debug_console */
void task_debug_console(void *argument)
{
  /* USER CODE BEGIN task_debug_console */
  /* Infinite loop */
  // for(;;)
  // {
  //   osDelay(1);
  // }
  task_app_console();
  /* USER CODE END task_debug_console */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

